<?php

class PHPMutagen_MutableFile
{

    protected $_fileName = null;
    protected $_mutations = array();

    public function __construct($file) 
    {
        $this->_fileName = $file;
    }

    public function generateMutations() 
    {
        $tokens = $this->_tokenize();
        $this->parseTokensToMutations($tokens);
    }

    public function getFilename() 
    {
        return $this->_fileName;
    }

    public function parseTokensToMutations(array $tokens) 
    {
        foreach ($tokens as $index=>$token) {
            if (is_string($token)) {
                $this->_parseStringToken($token, $index);
            } else {
                $this->_parseToken($token, $index);
            }
        }
    }

    public function getMutations() 
    {
        return $this->_mutations;
    }

    public function hasMutations() 
    {
        return (count($this->_mutations) > 0);
    }

    public function hasMutation($type) 
    {
        $typeClass = 'PHPMutagen_Mutation_' . $type;
        foreach ($this->_mutations as $mutation) {
            if ($mutation instanceof $typeClass) {
                return true;
            }
        }
        return false;
    }

    protected function _tokenize() 
    {
        $source = file_get_contents($this->getFilename());
        return token_get_all($source);
    }

    protected function _parseStringToken($token, $index) 
    {
        $type = '';
        switch ($token) {
            case '+':
                $type = 'OperatorAddition';
                break;
        }
        
        if (!empty($type)) {
            $mutationClass =  'PHPMutagen_Mutation_' . $type;
            $mutation = new $mutationClass($this->getFilename());
            $this->_mutations[$index] = $mutation;
        }
    }

    protected function _parseToken(array $token, $index) 
    {
        $type = '';
        switch ($token[0]) {
            case T_INC:
                $type = 'OperatorIncrement';
                break;
        }

        if (!empty($type)) {
            $mutationClass =  'PHPMutagen_Mutation_' . $type;
            $mutation = new $mutationClass($this->getFilename());
            $this->_mutations[$index] = $mutation;
        }
    }

}